<?php

namespace app\controller\api;

use app\base\BaseEncryptController;
use app\factory\Agency as AgencyFactory;
use app\model\agency\AgencyArticleModel;
use app\model\agency\AgencyModel;
use think\Request;


/**
 * Agent 代理商
 * Class Agent
 * @package app\controller
 */
class Agency extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new AgencyModel();
    }

    /**
     * 代理商个人中心
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onCore(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->core($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商收益提现
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onEarningsExtract(Request $request)
    {
        $price = $request->param('price');
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->earningsExtract($price);
        return self::returnMsg($res);
    }

    /**
     * 代理商收益
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onEarnings(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->earnings($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商库存和待发货
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStores(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->stores($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商订单物流设置
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onLogistics(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->logistics($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商转单给平台
     * @param $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onCareSystem(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->careSystem($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商获取物流列表
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onLogisticsList()
    {
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->logisticsList();
        return self::returnMsg($res);
    }

    /**
     * 代理商订单列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function onOrderList(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->orderList($formData);
        return self::returnMsg($res);
    }

    /**
     * 订单详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onOrderDetail(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $logistics_id = isset($formData['logistics_id']) ? $formData['logistics_id'] : '';
        $order = $AgencyFactory->orderDetail($formData['id'], $logistics_id);
        if (empty($order)) {
            return self::returnMsg(0, "订单不存在");
        }
        return self::returnMsg(1, $order, '获取成功');
    }


    /**
     * 订单发货详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onOrderSendDetail(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->orderSendDetail($formData);
        return self::returnMsg($res);
    }

    /**
     * 订单发货详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onAgentSendItem(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agentSendItem($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商退换订单处理
     * @param Request $request
     * @return \think\response\Json
     */
    public function onOutOrderHandle(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->outOrderHandle($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商退换订单列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function onAgencyOrderShow(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agencyOrderShow($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商生成进货单
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAgencyOrderCreate(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agencyOrderCreate($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商进货单列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAgencyOrderList(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agencyOrderList($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商获取物流订单数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAgentGetLogistics(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agentGetLogistics($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商进货单-取消
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAgencyOrderCancel(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agencyOrderCancel($formData);
        return self::returnMsg($res);
    }

    /**
     * 获取文章详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArticle(Request $request)
    {
        $id = $request->param('id');
        if (!empty($id)) {
            $article_model = new AgencyArticleModel();
            $data = $article_model->getInfo($id);
            return self::returnMsg(1, $data, "获取成功");
        }
        return self::returnMsg(0);
    }


    /**
     * 代理商 代理用戶列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onAgencyUserList(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->agencyUserList($formData);
        return self::returnMsg($res);
    }


    /**
     * 代理商设置代理用户的备注信息
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onSetUserRemark(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->setUserRemark($formData);
        return self::returnMsg($res);
    }

    /**
     * 代理商添加代理用户
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAddAgencyUser(Request $request)
    {
        $formData = $request->param();
        $userInfo = $this->currentUser;//登录信息
        $AgencyFactory = new AgencyFactory($userInfo);
        $res = $AgencyFactory->addAgencyUser($formData);
        return self::returnMsg($res);
    }
}
